package fct.mpc.utils;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.ViewGroup;

public class HuntingMapView extends ViewGroup implements LocationListener
{
	private Random randomizer = new Random();

	public HuntingMapView(Context context)
	{
		super(context);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		int w = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
		int h = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
		int sizeSpec;

		if (w > h)
			sizeSpec = MeasureSpec.makeMeasureSpec((int) (w * Math.sqrt(2)), MeasureSpec.EXACTLY);
		else
			sizeSpec = MeasureSpec.makeMeasureSpec((int) (h * Math.sqrt(2)), MeasureSpec.EXACTLY);

		int child = getChildCount();
		for (int i = 0; i < child; i++)
			getChildAt(i).measure(sizeSpec, sizeSpec);
		
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b)
	{
		int width = getWidth();
		int height = getHeight();
		int child = getChildCount();
		
		for (int i = 0; i < child; i++)
		{
			View view = getChildAt(i);
			
			int childWidth = view.getMeasuredWidth();
			int childHeight = view.getMeasuredHeight();
			int childLeft = (width - childWidth) / 2;
			int childTop = (height - childHeight) / 2;
			
			view.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        }
	}

	@Override
	public void onLocationChanged(Location location)
	{
		int lat = (int)(location.getLatitude() * 1E6);
		int lon = (int)(location.getLongitude() * 1E6);
		
		if(HuntingGrounds.nearSnipe(lat, lon))
		{
			Vibrator vibration = (Vibrator)getContext().getSystemService(Activity.VIBRATOR_SERVICE);
			vibration.vibrate(2000);

			ArrayList<Snipe> snipes = SnipeCollection.getAllSnipes();
			int index = randomizer.nextInt(snipes.size());

			AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
			alertDialog.setTitle("");
			alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener()
			{	
				public void onClick(DialogInterface dialog, int which)
				{
				}
			});

			Snipe snipe = snipes.get(index);
			if(!snipe.caught)
			{
				snipe.caught = true;
				alertDialog.setMessage("You have caught " + snipe.getName() + " the " + snipe.getElement() + " Snipe!");
			}
			else
				alertDialog.setMessage("You already have " + snipe.getName() + "...");
			
			alertDialog.show();

			HuntingGrounds.snipeCaught(lat, lon); 
		}
	}

	@Override
	public void onProviderDisabled(String arg0) {
	}

	@Override
	public void onProviderEnabled(String arg0) {
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
	}
}